import { Label, Node, Color, v3, Graphics } from 'cc'
import methods from "./methods";

const renderExample = {
    renderHp: undefined,
    renderHpDestroy: undefined,
    drawRectangle: undefined
}

// 生成生命值
renderExample.renderHp = (text: string, x: number, y: number): Node => {
    const hpExample = new Node(`${text}${x.toFixed(2)}${y.toFixed(2)}`)
    const hpComp = hpExample.addComponent(Label);
    hpComp.color = new Color(255, 255, 255, 255); // 白色
    hpComp.fontSize = 14
    hpComp.string = text;
    hpExample.setScale(0, 0, 0);
    hpExample.moveTop = 0
    hpExample.isBig = true
    hpExample.setPosition(methods.getRandomNumber(-10, 10) + x, y); // 设置位置
    return hpExample; // 返回Node而不是Label组件
}
// 销毁生命值动画
renderExample.renderHpDestroy = (hpExample: Node[]) => {
    let indexs = []
    for(let [index, i] of hpExample.entries()) {
        if(i.scale.x >= 0.9 || i.isBig) {
            let scale = i.isBig? i.scale.x + 0.1: i.scale.x - 0.1
            if(scale > 2) {
                i.isBig = false
            }
            i.scale = v3(scale, scale, scale)
        }
        // i.setOpacity(0.1);
        i.moveTop += 0.03
        i.y += i.moveTop
        if(i.moveTop >= 2) {
            i.destroy()
            indexs.push(index)
        }
    }
    methods.removeElementsByIndices(hpExample, indexs)
}

// 绘制矩形
renderExample.drawRectangle = (node: Node, x: number, y: number, width: number, height: number, color: Color) => {
    const frameNode = new Node(`Frame_${i}_${j}`);
    // 在指定节点上获取或添加 Graphics 组件
    const graphics = node.getComponent(Graphics) || node.addComponent(Graphics);
    // 设置填充颜色
    graphics.fillColor = color;
    // 绘制矩形路径
    graphics.rect(x, y, width, height);
    // 填充矩形
    graphics.fill();
    return graphics;
}

export default renderExample
